package com.example.sandbox.handler;

import com.alibaba.cola.dto.Response;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @description: 通用的 API 接口封装
 * @author: liuxinxin
 * @date: 2022/04/22
 */
@Data
@NoArgsConstructor
public class ApiResponse<T> extends Response {
    private T data;

    public static <T> ApiResponse<T> of(T data) {
        ApiResponse<T> apiResponse = new ApiResponse<>();
        apiResponse.setSuccess(true);
        apiResponse.setData(data);
        return apiResponse;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ApiResponse buildFailure(String errCode, String errMessage) {
        ApiResponse response = new ApiResponse();
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        return response;
    }

    public static ApiResponse buildSuccess() {
        ApiResponse response = new ApiResponse();
        response.setSuccess(true);
        return response;
    }
}
